<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header">
        <div class="logo">
          <img src="../../public/images/EasyGo_Logo.png" alt="Logo" width="118px" />
        </div>
      </el-header>
      <el-main>
        <el-card class="box-card">
          <template #header>
            <div class="card-header">管理员登录</div>
          </template>
          <div class="item">
            <el-form
              ref="ruleFormRef"
              :model="ruleForm"
              status-icon
              :rules="rules"
              label-width="120px"
              class="demo-ruleForm"
            >
              <el-form-item label="用户名" prop="username" label-width="auto">
                <el-input
                  v-model="ruleForm.username"
                  type="text"
                  autocomplete="off"
                />
              </el-form-item>
              <el-form-item label="密　码" prop="password" label-width="auto">
                <el-input
                  v-model="ruleForm.password"
                  type="password"
                  autocomplete="off"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)"
                  >登录</el-button
                >
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-main>
      <el-footer>
        <footer class="footer">
          <div class="container">
            <p>
              © {{ currentYear }} 浙江温州果宝三剑客网络有限公司. All rights
              reserved.
            </p>
            <ul class="links">
              <li><a href="https://www.zjitc.net/">关于我们</a></li>
              <li><a href="https://www.zjitc.net/">隐私政策</a></li>
              <li><a href="https://www.zjitc.net/">服务条款</a></li>
            </ul>
          </div>
        </footer>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { login } from '../api'
import { useRouter } from 'vue-router'
import useToken from '../stores/token'
import notification from '../utils/notification'

const router = useRouter()
const { updateToken } = useToken()

const currentYear = new Date().getFullYear();
const ruleFormRef = ref("");

const ruleForm = reactive({
  username: "",
  password: "",
});

const submitForm = formEl => {
  formEl.validate(async valid => {
    if (valid) {
      const data = await login(ruleForm)
      if (data) {
        updateToken(data.token)
        router.push({ name: 'index' })
      }
    } else {
      notification({
        message: '表单填写有误',
        type: 'error'
      })
    }
  })
}

const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 5, message: "长度在3-5个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 3, max: 9, message: "长度在3-9个字符", trigger: "blur" },
  ],
});

const resetForm = () => {
  ruleFormRef.value.resetFields();
};
</script>

<style scoped>
.header {
  height: 90px;
  padding-left: 350px;
}
.logo {
  position: relative;
  top: 10px;
}
.el-main {
  background-image: url("../../public/images/Login_Backgroud.jpg");
  background-position: 140px;
  height: 600px;
  background-size: cover;
  padding-right: 300px;
  padding-top: 150px;
}
.card-header {
  justify-content: space-between;
  align-items: center;
  font-size: 24px;
  text-align: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 400px;
  float: right;
}

.el-footer {
  padding-top: 75px;
}

.footer {
  color: black;
  padding: 20px 0;
  text-align: center;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.links {
  list-style-type: none;
  padding: 0;
}

.links li {
  display: inline;
  margin: 0 15px;
}

.links a {
  color: black;
  text-decoration: none;
}

.links a:hover {
  text-decoration: underline;
}
</style>
